#include <time.h>
#include "../include/head.h"
#include "../lib/error.h"

void mode_to_letters(int mode,char *str)
{
    strcpy(str,"----------");

    if (S_ISDIR(mode)) {
        str[0] = 'd';
    }
    if (S_ISCHR(mode)) {
        str[0] = 'c';
    }
    if (S_ISBLK(mode)) {
        str[0] = 'b';
    }

    if (mode & S_IRGRP) {
        str[1] = 'r';
    }
    if (mode & S_IWUSR) {
        str[2] = 'w';
    }
    if (mode & S_IXUSR) {
        str[3] = 'x';
    }

    if (mode & S_IRGRP) {
        str[4] = 'r';
    }
    if (mode & S_IWGRP) {
        str[5] = 'w';
    }
    if (mode & S_IXGRP) {
        str[6] = 'x';
    }

    if (mode & S_IROTH) {
        str[7] = 'r';
    }
    if (mode & S_IWOTH) {
        str[8] = 'w';
    }
    if (mode & S_IXOTH) {
        str[9] = 'x';
    }
    
}
char * uid_to_name(uid_t uid)
{
    struct passwd *pw_ptr;
    static char number[10];

    if ((pw_ptr = getpwuid(uid)) == NULL ) {
        sprintf(number,"%d",uid);
        return number;
    }
    return pw_ptr->pw_name;
}

char * gid_to_name(gid_t gid)
{
    struct group *grp_ptr;
    static char number[10];

    if ((grp_ptr = getgrgid(gid)) == NULL ) {
        sprintf(number,"%d",gid);
        return number;
    }
    return grp_ptr->gr_name;
}

void show_file_info(char *fname,struct stat *buf)
{
    char mode_str[11];
    mode_to_letters(buf->st_mode,mode_str);

    printf("%s",mode_str);
    printf("%4d ",buf->st_nlink);
    printf("%-8s",uid_to_name(buf->st_uid));
    printf("%-8s",gid_to_name(buf->st_gid));
    printf("%8ld  ",buf->st_size);
    printf("%.12s  ",4+ctime(&buf->st_mtime));
    printf("%s\n",fname);
    
}

void dostat(char * fname)
{
    struct stat info;

    if (stat(fname,&info) == -1) {
        perror(fname);
        exit(1);
    }
    else {
        show_file_info(fname,&info);
    }

}

void do_ls(char *dirName)
{
    DIR *pDir;

    struct dirent *pDirent;

    if ((pDir = opendir(dirName)) == NULL) {
        fprintf(stderr,"ls1:cannot open %s\n",dirName);
    }
    else {
        while ((pDirent = readdir(pDir)) != NULL) {
            dostat(pDirent->d_name);
        }
        closedir(pDir);
    }

}

int main(int argc,char *argv[])
{
    if (argc == 1) {
        do_ls(".");
    }
    else{
        while (--argc) {
            printf("%s:\n",*++argv);
            do_ls(*argv);
        }
    }

    return 0;
}

/*
// printf size of passwd file
int main()
{
    struct stat infobuf;

    if (stat("/etc/passwd",&infobuf) == -1) {
        perror("etc/passwd");
        exit(1);
    }
    else {
        printf("the size of /etc/passwd is %lld",infobuf.st_size);
    }
    return 0;
}

*/


/*
void show_stat_info(char *fname,struct stat *buf)
{
    printf("    mode: %o\n",buf->st_mode);
    printf("    link: %d\n",buf->st_nlink);
    printf("    user: %ld\n",buf->st_uid);
    printf("   group: %ld\n",buf->st_gid);
    printf("    size: %lld\n",buf->st_size);
    printf("modetime: %ld\n",buf->st_mtime);
    printf("    name: %s\n",fname);
    
}


int main(int argc,char *argv[])
{
    struct stat info;

    if (argc > 1) {
        if (stat(argv[1],&info) != -1) {
            show_stat_info(argv[1],&info);
            return 0;
        }
        else {
            perror(argv[1]);
            exit(1);
        }
    }
    return 1;
}

*/
